/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.example.soscall.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.IBinder;
import android.util.Log;

import com.ecarx.common.BaseApplication;
import com.ecarx.xui.adaptapi.Nullable;
import com.ecarx.xui.adaptapi.input.KeyCode;
import com.ecarx.xui.adaptapi.oncall.Call;
import com.ecarx.xui.adaptapi.oncall.OnCall;
import com.example.soscall.MyApplication;
import com.example.soscall.PhoneLiveData;
import com.example.soscall.utils.CallHelper;
import com.example.soscall.utils.TimeUtil;


public class XCallMainService extends Service {

    private static final String TAG = XCallMainService.class.getSimpleName();
    private static final String HOME_INTENT = "ecarx.action.ECARX_ACITON_HOME";
    private static final String ECARX_EXTRA_ECARX_KEY_EVENT_TYPE = "ecarx.extra.ECARX_KEY_EVENT_TYPE";
    private static final String ECARX_EXTRA_ECARX_KEY_ACTION_TYPE = "ecarx.extra.ECARX_KEY_ACTION_TYPE";
    private static final int ACTION_CLICK_SHORT = 0;

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, "onReceive: action: " + action);
            if (HOME_INTENT.equals(action)) {
                int key = intent.getIntExtra(ECARX_EXTRA_ECARX_KEY_EVENT_TYPE, -1);
                int mKeyAction = intent.getIntExtra(ECARX_EXTRA_ECARX_KEY_ACTION_TYPE, -1);
                if (KeyCode.KEYCODE_HOME == key && ACTION_CLICK_SHORT == mKeyAction) {
                    // 短按 Home 键
                    Log.i(TAG, " homekey click short");
                    Log.i(TAG, " 打上patch之后才会出现的log,后面会回退");
                    int state = CallHelper.getInstance().getTBoxCallSatus();
                    int type = CallHelper.getInstance().getTBoxCallType();
                    if (state == Call.CALL_STATUS_END || state == Call.CALL_STATUS_IDLE
                            || type == Call.CALL_TYPE_IDLE || state == Call.CALL_STATUS_IN_ECALL_CALLBACK
                            || state == Call.CALL_STATUS_START_FAILED || state == Call.CALL_STATUS_CONNECT_FAILED) {
                        CallHelper.getInstance().doTestCallStatusChanged(type, state);
//                        ConstantBase.IS_MAIN = false ;
                        return;
                    }
                    CallHelper.getInstance().doTestCallStatusChanged(type, state);
//                    WindowUtils.showCallLittlesizeWindow(type, state);
                }
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "XCallMainService onCreate");
        registerCall();
        IntentFilter mIntentFilter = new IntentFilter(HOME_INTENT);
        registerReceiver(mBroadcastReceiver, mIntentFilter);
    }

    private void registerCall() {
        Log.d(TAG, "XCallMainService registerCallListener");
        CallHelper.getInstance().registerCallListener();

        int callType = CallHelper.getInstance().getTBoxCallType();
        int callStates = CallHelper.getInstance().getTBoxCallSatus();
        if (callType == -1 || callStates == -1) {
            return;
        }

        //启动后检查有没有救援倒计时
        OnCall mOnCall = OnCall.create(MyApplication.sApp);
        if (mOnCall != null) {
            int rescueTime = mOnCall.getETARescue();
            if (rescueTime > 0) {
//                WindowUtils.showRescueWindow(MyApplication.sApp, callType, rescueTime);
            }
        }

        //如果在通话中也不在回拨模式，根据当前view的状态来决定是否显示view
        if (callStates != Call.CALL_STATUS_IN_ECALL_CALLBACK) {
            //没有界面显示就根据状态恢复界面
            CallHelper.getInstance().onCallStatusChangedUIThread(callType, callStates);
        }
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "XCallMainService onStartCommand");
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "XCallMainService onDestroy");
        CallHelper.getInstance().unregisterCallListener();
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.d(TAG, "XCallMainService onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
        int callType = CallHelper.getInstance().getTBoxCallType();
        int callStates = CallHelper.getInstance().getTBoxCallSatus();
        CallHelper.getInstance().onCallStatusChangedUIThread(callType, callStates);
    }
}
